package com.woniuxy.controller;

import com.woniuxy.dto.MyDto;
import com.woniuxy.pojo.MerchantAuth;
import com.woniuxy.pojo.MerchantUsers;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class MerchantUsersController {
    @Autowired
    private UserService userService;

    /**
     * 增加用户
     * @param users
     * @return
     */
    @PostMapping("/addpuser")
    @ResponseBody
    public String add(MerchantUsers users){
        users.setmState("-1");
        try {
            userService.addpUser(users);
           return "增加成功";
        }catch (Exception e){
           return "增加失败";
        }
    }


    /**
     * 提交审核订单
     * @param users
     * @return
     */
    @PostMapping("/register.do")
    @ResponseBody
    public MyDto register(MerchantAuth users){
        System.out.println(users.getmName());
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        MerchantUsers iter=(MerchantUsers) session.getAttribute("msg");
        users.setmAddress(iter.getmAddress());
        users.setLnglat(iter.getLnglat());
        MyDto myDto=new MyDto();
        users.setmId(user.getId());

        try {
            userService.register(users);
            myDto.setCode(200);
            myDto.setMsg("认证中");
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);
            myDto.setMsg("信息填写有问题");
        }
        return  myDto;
    }
    @GetMapping("/findmerchantuser")
    @ResponseBody
    public MyDto find(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        MerchantUsers merchantUsers =userService.findPuserByid(user.getId());
        MyDto myDto=new MyDto(200,"");
        myDto.setData(merchantUsers);
        return myDto;
    }
    @PostMapping("/lnglat.do")
    @ResponseBody
    public  MyDto save(MerchantUsers users){
        MyDto myDto=new MyDto();
      try {
          Subject subject= SecurityUtils.getSubject();
          Session session=subject.getSession();
          session.setAttribute("msg",users);
          myDto.setCode(200);
          myDto.setMsg("地址设置成功");
      }catch (Exception e){
          e.printStackTrace();
          myDto.setCode(-200);  myDto.setMsg("地址设置失败");
      }
     return myDto;
    }
}
